package com.example.superoa.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.pojos.Qingjia;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.DemoService;
import com.example.superoa.model.service.LeaveClassService;
import com.example.superoa.model.service.LeaveService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class LeaveController {
	@Autowired
	private LeaveService ser;
	
	@Autowired
	private LeaveClassService l_ser;
	
	
	@Autowired
	private DemoService d_ser;
	
	
	
	private Shijian sj=new Shijian();
	//kq-leave-assessor 页面table初始化值
//	@RequestMapping("leave_selectAll")
//	@ResponseBody
//	public Map<String,Object> LeaveSelectAll(int page,int rows) {
//		Map<String,Object> map = new HashMap<>();
//		//先分页
//		Page<Object> pg = PageHelper.startPage(page, rows);
//		JSONArray json = ser.selectAll();
//		int i=1;
//		for (Object o : json) {
//			String str="";
//			String str1="";
//			JSONObject obj=(JSONObject)o;
//			int shen=obj.getInteger("L_SHEN");
//			Integer idd=obj.getInteger("L_IDD");
//			int type=obj.getInteger("L_CLASS");
//			if(type==2) {
//				str1="带薪";
//			}else {
//				if(ser.selectIDD(obj.getInteger("L_ID"))!=null) {
//					str1="扣薪含带薪";
//				}else {
//					str1="扣薪";
//				}
//			}
//			
//			if(shen==0) {
//				str="未审核";
//			}else if(shen==1) {
//				str="通过";
//			}else {
//				str="未通过";
//			}
//			obj.put("l_xtype", str1);
//			obj.put("l_sta", str);
//			obj.put("sortable", i);
//			i++;
//		}
//		//总记录
//		map.put("total", pg.getTotal());
//		//页内容
//		map.put("rows", json);
//		return map;
//	}
	//kq-leave-statistical 页面table初始化值
	@RequestMapping("qstatis_selectAll")
	@ResponseBody
	public Map<String,Object> statisSelectAll(Date date,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		if(date==null) {
			date=new Date(sj.getNetworkTime());
		}
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json =new JSONArray();
		JSONArray list=d_ser.selectKuang();
		JSONArray arr=ser.selectAllMonth(date);
		int i=1;
		for (Object o : list) {
			JSONObject obj=new JSONObject();
			JSONObject ob=(JSONObject)o;
			JSONObject objj=(JSONObject)arr.get(i-1);
			obj.put("sortable", i);
			obj.put("staff_id", ob.getInteger("staff_id"));
			obj.put("Staff_name", ob.getString("Staff_name"));
			obj.put("depart_name", ob.getString("depart_name"));
			obj.put("shi",objj.getInteger("shi"));
			obj.put("bing",objj.getInteger("bing"));
			obj.put("xiu",objj.getInteger("xiu"));
			obj.put("qi",objj.getInteger("qi"));
			i++;
			json.add(obj);
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}	
	//apply  提交按钮，新增请假记录
	@RequestMapping("add_leave")
	@ResponseBody
	public String add(Qingjia leave,int tian,HttpSession session){
		leave.setLJie(sj.getDaiDate(leave.getLKai(), tian-1));
		ser.addLeave(leave, tian,Integer.parseInt(session.getAttribute("sid").toString()));
		return "";
	}
	//apply 提交判断是否有重复
	@RequestMapping("select-qing-pan")
	@ResponseBody
	public Integer panduan(Date date,Integer time,HttpSession session){
		return ser.panduan(date,time,Integer.parseInt(session.getAttribute("sid").toString()));
	}
	//kq-leave-assessor  查看
	@RequestMapping("kq-leave-cha")
	public void cha(Integer id,Model model) {
		JSONObject qing=ser.selectid(id);
		Staff staff=d_ser.selectStaffid(qing.getInteger("staff_id"));
		model.addAttribute("qing", qing);
		model.addAttribute("type", l_ser.selectid(qing.getInteger("LT_ID")).getLtName());
		model.addAttribute("staff", staff);
	}
}
